package edu.ccut.saturn.studio.logic.model.saturn;

import java.util.HashMap;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public abstract class SaturnBaseComponentNode extends Shape {

	private String key = "edu.ccut.saturn.basecomponent::/dataOperationComponent/SetDataValues.component";

	private HashMap<String, List<SaturnDbDataElement>> workspaceMap;

	private HashMap<String, List<SaturnDbDataElement>> projectMap;
	@SuppressWarnings("unused")
	private String exception = "";

	protected String fullKey = "";

	private Image image = Activator.getImage(Images.COMPONENT_ICON);

	public void setFullKey(String fullKey) {
		if (!this.fullKey.equals(fullKey)) {
			this.fullKey = fullKey;
			firePropertyChange(PROPERTERY_CHANGS, null, fullKey);
		}
	}

	public void setException(String exception) {
		this.exception = exception;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		if (!this.key.equals(key)) {
			this.key = key;
			firePropertyChange(PROPERTERY_CHANGS, null, key);
		}
	}

	public Node doRefresh() {
		Node root = super.getRoot();

		Node node = SaturnW3CUtil.createChildNode(root, "component", null);

		SaturnW3CUtil.createAttribute(node, "exception", getException());
		SaturnW3CUtil.createChildNode(node, "name", super.getName());
		SaturnW3CUtil.createChildNode(node, "key", getKey());

		createParemeters(node);
		creatConfig(node);

		return node;
	}

	public abstract String getType();

	public abstract String getFullKey();

	public abstract String getException();

	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	public Image getIcon() {
		return image;
	}

	protected void creatConfig(Node node) {
		Node config = SaturnW3CUtil.createChildNode(node, "configuration", null);
		SaturnW3CUtil.createAttribute(config, "type", getType());
		if (getFullKey() != null) {
			SaturnW3CUtil.createAttribute(config, "fullKey", getFullKey());
		}
	}

	protected void getProject() {

	}

	@SuppressWarnings("unused")
	private void creatParameter(Document document, Node node,
			List<SaturnParameter> parameters) {
		Node paras = document.createElement("parameters");
		for (SaturnParameter parameter : parameters) {
			Node parameterNode = document.createElement("parameter");
			SaturnW3CUtil
					.createAttribute(parameterNode, "id", parameter.getId());
			SaturnW3CUtil.createAttribute(parameterNode, "name", parameter
					.getName());
			SaturnW3CUtil.createAttribute(parameterNode, "classType", parameter
					.getClassType());
			SaturnW3CUtil.createAttribute(parameterNode, "passingType",
					parameter.getPassingType());
			SaturnW3CUtil.createChildNode(parameterNode, "value-key", parameter
					.getValueKey());
			SaturnW3CUtil.createChildNode(parameterNode, "scope", parameter
					.getScope());
			SaturnW3CUtil.createChildNode(parameterNode, "description",
					parameter.getDescription());

			paras.appendChild(parameterNode);
		}
		node.appendChild(paras);
	}

//	public HashMap<String, List<SaturnDbDataElement>> getMapfromProject() {
//		HashMap<String, List<SaturnDbDataElement>> map = new HashMap<String, List<SaturnDbDataElement>>();
//
//		List<String> keys = new ArrayList<String>();
//
//		if ((!(this.getKey().equals("")) || this.getKey() != null)) {
//			keys.add(this.getKey());
//		}
//		IProject project = super.findTheProject();
//		List<Document> documents = new ArrayList<Document>();
//
//		String temp[] = new String[] { "saturndata", "sqldata" };
//		documents = SearchFile.searchFileInproject(project, temp);
//		for (Document document : documents) {
//			String namespace = ImplInSaturnNode.getSaturnNodeKeyInDocument(
//					document, "data", "namespace");
//			String name = ImplInSaturnNode.getSaturnNodeKeyInDocument(document,
//					"data", "name");
//			String key = namespace + "::" + name;
//			List<SaturnDbDataElement> list = DbData.getColumnNodeList(document
//					.getDocumentElement());
//
//			map.put(key, list);
//		}
//		this.setProjectMap(map);
//		return map;
//	}
//
//	public HashMap<String, List<SaturnDbDataElement>> getMapFormWorkspace() {
//
//		HashMap<String, List<SaturnDbDataElement>> map = new HashMap<String, List<SaturnDbDataElement>>();
//
//		List<String> keys = new ArrayList<String>();
//
//		if ((!(this.getKey().equals("")) || this.getKey() != null)) {
//			keys.add(this.getKey());
//		}
//		List<Document> documents = new ArrayList<Document>();
//
//		String temp[] = new String[] { "saturndata", "sqldata" };
//		documents = SearchFile.searchFileInWorkspace(temp);
//
//		for (Document document : documents) {
//			String namespace = ImplInSaturnNode.getSaturnNodeKeyInDocument(
//					document, "data", "namespace");
//			String name = ImplInSaturnNode.getSaturnNodeKeyInDocument(document,
//					"data", "name");
//			String key = namespace + "::" + name;
//			List<SaturnDbDataElement> list = DbData.getColumnNodeList(document
//					.getDocumentElement());
//
//			map.put(key, list);
//		}
//		this.setWorkspaceMap(map);
//		return map;
//	}

	public HashMap<String, List<SaturnDbDataElement>> getWorkspaceMap() {
		return workspaceMap;
	}

	public void setWorkspaceMap(
			HashMap<String, List<SaturnDbDataElement>> workspaceMap) {
		this.workspaceMap = workspaceMap;
	}

	public HashMap<String, List<SaturnDbDataElement>> getProjectMap() {
		return projectMap;
	}

	public void setProjectMap(
			HashMap<String, List<SaturnDbDataElement>> projectMap) {
		this.projectMap = projectMap;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}
}
